package ru.ognick.option;

import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import org.json.JSONException;

import ru.ognick.R;
import ru.ognick.locale.ILocale;
import ru.ognick.locale.Locale;
import ru.ognick.logic.Praxis;
import ru.ognick.logic.Training;
import ru.ognick.utils.FileManager;
import ru.ognick.utils.Media;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

public class EditorActivity extends Activity {
	public static final String TAG = "EditorActivity";
	final public static int PICK_REQUEST_CODE = 0;
	// private String fileName = null;
	final LayotBuilder viewBuilder = new LayotBuilder(this);
	final private EditorActivity self = this;
	final ILocale L = Locale.get();
	
    ListView musiclist;
    Cursor musiccursor;
    int music_column_index;
    int count;
    MediaPlayer mMediaPlayer;
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		setContentView(R.layout.editor);
		
		final PraxisAdapter praxisAdapter = new PraxisAdapter(this,
				android.R.layout.simple_list_item_1, new ArrayList<BundleEditor>());

		final File file = (File) getIntent().getExtras().get("FILE");
		try {
			Training training = FileManager.loadTraining(file);
			for (Praxis praxis : training) {
				LinearLayout layout = viewBuilder.createLayout(praxis);
				final BundleEditor bundleEditor = new BundleEditor(praxis, layout);
				praxisAdapter.add(bundleEditor);

				Button upButton = (Button) layout.findViewWithTag("UP_BUTTON");
				upButton.setOnClickListener(new OnClickListener() {
				
					public void onClick(View v) {
						int i = praxisAdapter.getPosition(bundleEditor);
						if (i > 0) {
							praxisAdapter.remove(bundleEditor);
							praxisAdapter.insert(bundleEditor, --i);
						}
							
					}
				});
				
				Button downButton = (Button) layout.findViewWithTag("DOWN_BUTTON");
				downButton.setOnClickListener(new OnClickListener() {
				
					public void onClick(View v) {
						int i = praxisAdapter.getPosition(bundleEditor);
						if (i < praxisAdapter.getCount() - 1) {
							praxisAdapter.remove(bundleEditor);
							praxisAdapter.insert(bundleEditor, ++i);
						}
							
					}
				});
				
				Button delButton = (Button) layout.findViewWithTag("DEL_BUTTON");
				delButton.setOnClickListener(new OnClickListener() {
				
					public void onClick(View v) {
					   praxisAdapter.remove(bundleEditor);
					}
				});
			}

			final ListView content = (ListView) findViewById(R.id.listView1);
			content.setAdapter(praxisAdapter);
			
			Button saveButton = (Button) findViewById(R.id.button2);
			saveButton.setText(L.G_BUTTON_SAVE());
			saveButton.setOnClickListener(new OnClickListener() {
			
				public void onClick(View v) {
					praxisAdapter.save(file);
					self.finish();
				}
			});
			
			Button abortButton = (Button) findViewById(R.id.button3);
			abortButton.setText(L.G_BUTTON_ABORT());
			abortButton.setOnClickListener(new OnClickListener() {
			
				public void onClick(View v) {
					self.finish();
				}
			});

			Button newPraxis = (Button) findViewById(R.id.button1);
			newPraxis.setText(L.G_BUTTON_NEW_PRAXIS());
			newPraxis.setOnClickListener(new OnClickListener() {
			
				public void onClick(View v) {
					Praxis praxis = new Praxis();
					LinearLayout layout = viewBuilder.createLayout(praxis); 
					praxisAdapter.add(new BundleEditor(praxis, layout));
					content.requestChildFocus(layout, layout);
				}
			});
			
			
			
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage());
		}
	}



    
    private class Mp3Filter implements FilenameFilter {
        public boolean accept(File dir, String name) {
                return (name.endsWith(".mp3"));
        }
    }
    
	protected Dialog onCreateDialog(int id) {
	    final List<CharSequence> songs = new ArrayList<CharSequence>();
	    int index = 0;
	    songs.add("No selected");
	    Dialog dialog = null;
	    switch(id) {
	    case PICK_REQUEST_CODE:
		    File home = new File(Media.MEDIA_PATH);
		    if (home.listFiles(new Mp3Filter()).length > 0) {
		            for (File file : home.listFiles(new Mp3Filter())) {
		            		String name = file.getName();
		            		if (viewBuilder.cur_media_button != null && 
		            			name.equals(viewBuilder.cur_media_button.getText()))
		            				index = songs.size();
		                    songs.add(name);
		            }
		    }
		    
		    AlertDialog.Builder builder = new AlertDialog.Builder(this);
		    builder.setTitle("Sound choose");
		    CharSequence[] arr = songs.toArray(new CharSequence[songs.size()]);
		    builder.setSingleChoiceItems(arr,	index, new	DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (viewBuilder.cur_media_button != null)
						viewBuilder.cur_media_button.setText((which == 0) ? null : songs.get(which));
					viewBuilder.cur_media_button = null;
					dialog.cancel();
				}
			})
			.setCancelable(false);
		    builder.setOnCancelListener(new OnCancelListener() {
				public void onCancel(DialogInterface arg0) {
					if (viewBuilder.cur_media_button != null)
						viewBuilder.cur_media_button.setText(null);	
				    viewBuilder.cur_media_button = null;
				}
			});
		    dialog = builder.create();
	        break;
	    default:
	        dialog = null;
	    }
	    return dialog;
	}




}